﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using Model;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class CableTrayVolumeRatioForm : System.Windows.Forms.Form
	{
		public CableTrayVolumeRatioForm(UIDocument uiDoc)
		{
			this.InitializeComponent();
			this.m_uiDoc = uiDoc;
			this.m_Doc = uiDoc.ActiveView.Document;
		}

		private void CableTrayVolumeRatioForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.txtRatio.MostValue = new MostValue(0.0, 1.0);
			this.InitButtonImage();
			this.ReadFormDataFromXml();
		}

		private void InitButtonImage()
		{
			this.buttonConnect.Image = YArchitech.Electric.Resources.GetImage("QueryByConnect16");
			this.buttonAll.Image = YArchitech.Electric.Resources.GetImage("QueryAll16");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.buttonConnect, "按连接查询");
			toolTip.SetToolTip(this.buttonAll, "查询全部");
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			List<XmlElement> elements = xmlHelper.GetElements("CableLayRule");
			int num = (int)Convert.ToInt16(xmlHelper.GetAttributeEx("SelectedIndex", "0"));
			foreach (XmlElement xmlElement in elements)
			{
				string attribute = xmlElement.GetAttribute("Name");
				string attribute2 = xmlElement.GetAttribute("Value");
				this.cmbSystemType.Items.Add(new HyKeyValuePair<string, string>(attribute, attribute2));
			}
			if (this.cmbSystemType.Items.Count > num)
			{
				this.cmbSystemType.SelectedIndex = num;
			}
			else if (this.cmbSystemType.Items.Count > 0)
			{
				this.cmbSystemType.SelectedIndex = 0;
			}
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
			base.Location = new System.Drawing.Point(x, y);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("CableLayRule");
			xmlHelper.SetAttribute("SelectedIndex", this.cmbSystemType.SelectedIndex.ToString());
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml("");
		}

		private void cmbSystemType_SelectedIndexChanged(object sender, EventArgs e)
		{
			object selectedItem = this.cmbSystemType.SelectedItem;
			if (selectedItem != null)
			{
				HyKeyValuePair<string, string> hyKeyValuePair = selectedItem as HyKeyValuePair<string, string>;
				this.txtRatio.Text = hyKeyValuePair.Value;
			}
		}

		private void buttonConnect_Click(object sender, EventArgs e)
		{
			CableTray cableTray = this.SelectCableTray();
			if (cableTray == null || base.IsDisposed)
			{
				return;
			}
			IList<Element> pathwayDevices = new PathwayCheck().GetPathwayDevices(cableTray, SearchDeviceType.CableTray);
			IList<Element> entList = this.FilterCableTray(pathwayDevices);
			this.UpdateListView(entList);
		}

		private void buttonAll_Click(object sender, EventArgs e)
		{
			IList<Element> entList = new FilteredElementCollector(this.m_Doc).OfCategory(BuiltInCategory.OST_CableTray).OfClass(typeof(CableTray)).ToElements();
			IList<Element> entList2 = this.FilterCableTray(entList);
			this.UpdateListView(entList2);
		}

		private void listView_DoubleClick(object sender, EventArgs e)
		{
			if (this.listView.SelectedIndices.Count <= 0)
			{
				return;
			}
			ListViewItem listViewItem = this.listView.SelectedItems[0];
			if (listViewItem == null)
			{
				return;
			}
			object tag = listViewItem.Tag;
			if (tag == null)
			{
				return;
			}
			ElementId elementId = tag as ElementId;
			if (null == elementId)
			{
				return;
			}
			Element element = this.m_Doc.GetElement(elementId);
			if (element == null || !element.IsValidObject)
			{
				return;
			}
			if (ViewUtils.ElementIsShowInView(this.m_Doc, elementId, BuiltInCategory.OST_CableTray, this.m_uiDoc.ActiveView.Id))
			{
				CableLayRevitCommon.HighlightElement(this.m_uiDoc, element);
				return;
			}
			List<ElementId> viewsOpened = ViewUtils.GetViewsOpened(this.m_uiDoc, (CmdViewType)19);
			foreach (ElementId viewId in viewsOpened)
			{
				if (ViewUtils.ElementIsShowInView(this.m_Doc, elementId, BuiltInCategory.OST_CableTray, viewId))
				{
					CableLayRevitCommon.HighlightElement(this.m_uiDoc, element);
					return;
				}
			}
			foreach (ElementId elementId2 in ViewUtils.GetViews(this.m_Doc, (CmdViewType)3))
			{
				if (!viewsOpened.Contains(elementId2) && ViewUtils.ElementIsShowInView(this.m_Doc, elementId, BuiltInCategory.OST_CableTray, elementId2))
				{
                    Autodesk.Revit.DB.View view = this.m_Doc.GetElement(elementId2) as Autodesk.Revit.DB.View;
					if (view != null)
					{
						this.m_uiDoc.ActiveView = view;
						CableLayRevitCommon.HighlightElement(this.m_uiDoc, element);
						return;
					}
				}
			}
			foreach (ElementId elementId3 in ViewUtils.GetViews(this.m_Doc, CmdViewType.ThreeD))
			{
				if (!viewsOpened.Contains(elementId3) && ViewUtils.ElementIsShowInView(this.m_Doc, elementId, BuiltInCategory.OST_CableTray, elementId3))
				{
                    Autodesk.Revit.DB.View view2 = this.m_Doc.GetElement(elementId3) as Autodesk.Revit.DB.View;
					if (view2 != null)
					{
						this.m_uiDoc.ActiveView = view2;
						CableLayRevitCommon.HighlightElement(this.m_uiDoc, element);
						break;
					}
				}
			}
		}

		private CableTray SelectCableTray()
		{
			object selectedItem = this.cmbSystemType.SelectedItem;
			if (selectedItem == null)
			{
				return null;
			}
			HyKeyValuePair<string, string> hyKeyValuePair = selectedItem as HyKeyValuePair<string, string>;
			CableTray result;
			try
			{
				string text = "请选择指定系统类型的任一桥架，以此桥架为基础搜索所有相连桥架";
				Reference reference = this.m_uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new SelectionCableTrayFilter(hyKeyValuePair.Key.Trim()), text);
				if (reference == null)
				{
					result = null;
				}
				else
				{
					result = (this.m_Doc.GetElement(reference) as CableTray);
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private IList<Element> FilterCableTray(IList<Element> entList)
		{
			List<Element> list = new List<Element>();
			if (entList == null)
			{
				return list;
			}
			object selectedItem = this.cmbSystemType.SelectedItem;
			if (selectedItem == null)
			{
				return null;
			}
			HyKeyValuePair<string, string> hyKeyValuePair = selectedItem as HyKeyValuePair<string, string>;
			foreach (Element element in entList)
			{
				CableTray cableTray = element as CableTray;
				if (cableTray != null)
				{
					string text = cableTray.get_Parameter(BuiltInParameter.RBS_CTC_SERVICE_TYPE).AsString();
					if (!string.IsNullOrEmpty(text) && text.Trim() == hyKeyValuePair.Key.Trim())
					{
						list.Add(element);
					}
				}
			}
			return list;
		}

		private void UpdateListView(IList<Element> entList)
		{
			this.listView.Items.Clear();
			if (entList == null)
			{
				return;
			}
			double num = Convert.ToDouble(this.txtRatio.Text);
			this.listView.BeginUpdate();
			foreach (Element element in entList)
			{
				CableTray cableTray = element as CableTray;
				if (cableTray != null)
				{
					string text = string.Empty;
					Parameter parameter = cableTray.GetParameter("编号");
					if (parameter != null)
					{
						text = parameter.AsString();
					}
					if (string.IsNullOrEmpty(text))
					{
						text = "未编号";
					}
                    double num2 = (double)Convert.ToInt16(UnitConvert.CovertFromAPI((DisplayUnitType)2, cableTray.Width));
                    int num3 = (int)Convert.ToInt16(UnitConvert.CovertFromAPI((DisplayUnitType)2, cableTray.Height));
					double num4 = num2 * (double)num3;
					List<CableTraySchemaData> list = new List<CableTraySchemaData>();
					if (CableTraySchemaDataManager.GetData(element, ref list))
					{
						double num5 = 0.0;
						foreach (CableTraySchemaData cableTraySchemaData in list)
						{
							num5 += cableTraySchemaData.Section;
						}
						if (num5 / num4 > num)
						{
							ListViewItem listViewItem = new ListViewItem();
							listViewItem.Text = text;
							listViewItem.Tag = cableTray.Id;
							this.listView.Items.Add(listViewItem);
						}
					}
				}
			}
			this.listView.EndUpdate();
		}

		private const string XmlNode_Root = "CableLayRule";

		private const string Att_SelectedIndex = "SelectedIndex";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private UIDocument m_uiDoc;

		private Document m_Doc;
	}
}
